package us.icitap.commandcenter.ui.dispatcher;

import java.util.Date;
import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallReason;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.business.ejb.persistence.Officer;
import us.icitap.commandcenter.business.ejb.persistence.PoliceAction;
import us.icitap.commandcenter.business.ejb.persistence.UniqueNumber;
import us.icitap.commandcenter.listeners.CloseVerificationListener;
import us.icitap.commandcenter.listeners.FillListener;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustBeanFieldGroup;
import us.icitap.commandcenter.ui.custom.CustComboBox;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.utils.HandleNotification;
import us.icitap.commandcenter.utils.OfficerComponent;
import us.icitap.commandcenter.utils.ProjectLists;
import us.icitap.utils.Result;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.NullValidator;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class CloseVerificationWindow extends Window implements ClickListener {

	private Call call;
	private CloseVerificationListener closeVerificationListener;
	
	CustBeanFieldGroup<Call> binder = new CustBeanFieldGroup<Call>(Call.class);

	
	private final CheckBox falseCallCheckBox = new CheckBox("Eshte telefonate e rreme");
	private final CheckBox generateUNCheckBox = new CheckBox("Dua te krijoj numrin unik");
	
	private final CustTextArea verificationNotesTextArea = new CustTextArea("Shenime mbi verifikimin e telefonates");
	private final CustSaveButton saveButton = new CustSaveButton("RUAJ");
	
	private final CustComboBox verifiedCallReasonComboBox = new CustComboBox("Rezultati i verifikimit");
	
	private TwinColSelect policeActionTwinColSelect = new TwinColSelect();
	
	VerticalLayout contentLayout = new VerticalLayout();
	
	final UniqueNumberContent unContent = new UniqueNumberContent("Gjenerimi i numrit unik");

	public CloseVerificationWindow(CloseVerificationListener closeVerificationListener, Call call) {
	
		this.closeVerificationListener = closeVerificationListener;
		this.setCall(call);
		
		
		this.setCaption("Te dhena shtese per telefonaten");
		this.setModal(true);
		this.center();
		this.setResizable(false);
		
		binder.bind(falseCallCheckBox, "falseCall");
		binder.bind(verificationNotesTextArea, "verificationNotes");
		binder.bind(verifiedCallReasonComboBox, "verifiedCallReason");
		binder.bind(policeActionTwinColSelect, "policeActionList");
		
		
		
		contentLayout.setMargin(true);
		contentLayout.setSpacing(true);
		

		contentLayout.addComponent(falseCallCheckBox);
		
		this.verificationNotesTextArea.setWidth("450px");
		this.verificationNotesTextArea.setRows(4);
		contentLayout.addComponent(verificationNotesTextArea);

		contentLayout.addComponent(verifiedCallReasonComboBox);
		
		List<CallReason> callReasonList =  ProjectLists.getCallReasonList(false);
		
		verifiedCallReasonComboBox.setContainerDataSource(new BeanItemContainer<CallReason>(CallReason.class, callReasonList));
		verifiedCallReasonComboBox.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		verifiedCallReasonComboBox.setItemCaptionPropertyId("tag");
		
		
		
		policeActionTwinColSelect.setWidth("500px");
		policeActionTwinColSelect.setLeftColumnCaption("Lista e masave te mundshme");
		policeActionTwinColSelect.setRightColumnCaption("Lista e masave te marra");
		policeActionTwinColSelect.setImmediate(true);
		policeActionTwinColSelect.setNullSelectionAllowed(true);
		policeActionTwinColSelect.setContainerDataSource(new BeanItemContainer<PoliceAction>(PoliceAction.class, ProjectLists.getPoliceActionList()));
		policeActionTwinColSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT_DEFAULTS_ID);
		policeActionTwinColSelect.setItemCaptionPropertyId("tag");
		
		contentLayout.addComponent(policeActionTwinColSelect);
		
		generateUNCheckBox.setImmediate(true);
		generateUNCheckBox.setValue(new Boolean(false));
		contentLayout.addComponent(generateUNCheckBox);
		
//		contentLayout.addComponent(unContent);
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("500px");
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		contentLayout.addComponent(buttonsLayout);

		this.setContent(contentLayout);
		
		this.saveButton.addClickListener(this);
		falseCallCheckBox.setImmediate(true);
		falseCallCheckBox.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty() != null){
					Boolean val = (Boolean)event.getProperty().getValue();
					if(!val.booleanValue()){
						contentLayout.removeComponent(buttonsLayout);
						contentLayout.addComponent(generateUNCheckBox);
						contentLayout.addComponent(buttonsLayout);
						
						verifiedCallReasonComboBox.addValidator(new NullValidator("Arsyeja e telefonates pas verifikimit nuk duhet te jete bosh", false));
					}
					else{
						contentLayout.removeComponent(unContent);
						generateUNCheckBox.setValue(new Boolean(false));
						contentLayout.removeComponent(generateUNCheckBox);
						verifiedCallReasonComboBox.removeAllValidators();
						
					}
				}
					
				
			}
		});
		
		generateUNCheckBox.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty() != null){
					Boolean val = (Boolean)event.getProperty().getValue();
					if(val.booleanValue()){
						contentLayout.removeComponent(buttonsLayout);
						contentLayout.addComponent(unContent);
						contentLayout.addComponent(buttonsLayout);
						center();
	
					}
					else{
						contentLayout.removeComponent(unContent);
						
					}
				}
					
				
			}
		});


	}
	
	
	public void setCall(Call call){
		this.call = call;
		
		binder.setItemDataSource(this.call);
		verifiedCallReasonComboBox.removeAllValidators();
		
		
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		try {
			
			binder.commit();
			call.setVerificationUser(CommandCenterUI.getUser().getUsername());
			call.setActualStatus(CallStatusEnum.V);
			call.setVerificationDate(new Date());
			
			if(generateUNCheckBox.getValue().booleanValue()){
				Result<UniqueNumber> result = unContent.createUniqueNumberObject();
				if(result.isSucceeded()){
					UniqueNumber un = result.getObject();
					call.setUniqueNumber(un);
				}
				else {
					HandleNotification.showInfoOnTray(result.getMessage());
					return;
				}
			}

			this.closeVerificationListener.closeCallVerification(call);
			
		} catch (Exception e) {
			HandleNotification.showWarningOnScreen(e.getCause().getMessage());
		}
		
	}




}
